/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "Value = {Value}, IsEstimate = {IsEstimate}" )]
  internal class EstimateInt64
  {
    internal EstimateInt64( long value )
      : this( value, false )
    {
    }

    internal EstimateInt64( long value, bool isEstimate )
    {
      this.Value = value;
      this.IsEstimate = isEstimate;
    }


    #region Value Property

    internal long Value
    {
      get;
      private set;
    }

    #endregion Value Property

    #region IsEstimate Property

    internal bool IsEstimate
    {
      get;
      private set;
    }

    #endregion IsEstimate Property


#if DEBUG
    public override string ToString()
    {
      return string.Format( "(Val={0}, Est={1})", this.Value, this.IsEstimate );
    }
#endif

    public static implicit operator EstimateInt64( long value )
    {
      return new EstimateInt64( value, false );
    }

    public static bool operator ==( EstimateInt64 x, EstimateInt64 y )
    {
      if( object.ReferenceEquals( x, y ) )
        return true;

      if( object.ReferenceEquals( x, null ) )
        return y.Equals( x );

      return x.Equals( y );
    }

    public static bool operator !=( EstimateInt64 x, EstimateInt64 y )
    {
      return !( x == y );
    }

    public override int GetHashCode()
    {
      return this.Value.GetHashCode();
    }

    public override bool Equals( object obj )
    {
      EstimateInt64 value = obj as EstimateInt64;

      if( object.ReferenceEquals( value, null ) )
        return false;

      if( object.ReferenceEquals( value, this ) )
        return true;

      if( value.GetHashCode() != this.GetHashCode() )
        return false;

      return object.Equals( value.Value, this.Value )
          && object.Equals( value.IsEstimate, this.IsEstimate );
    }

    public static EstimateInt64 operator +( EstimateInt64 first, EstimateInt64 second )
    {
      EstimateInt64.CheckForNull( first, "first" );
      EstimateInt64.CheckForNull( second, "second" );

      return ( EstimateInt64.IsAdditiveIdentity( second ) )
               ? first
               : new EstimateInt64(
                       first.Value + second.Value,
                       EstimateInt64.IsBecomingEstimate( first, second ) );
    }

    public static EstimateInt64 operator +( EstimateInt64 value, long scalar )
    {
      EstimateInt64.CheckForNull( value, "value" );

      return ( EstimateInt64.IsAdditiveIdentity( scalar ) )
               ? value
               : new EstimateInt64( value.Value + scalar, value.IsEstimate );
    }

    public static EstimateInt64 operator -( EstimateInt64 first, EstimateInt64 second )
    {
      EstimateInt64.CheckForNull( first, "first" );
      EstimateInt64.CheckForNull( second, "second" );

      return ( EstimateInt64.IsAdditiveIdentity( second ) )
               ? first
               : new EstimateInt64(
                       first.Value - second.Value,
                       EstimateInt64.IsBecomingEstimate( first, second ) );
    }

    public static EstimateInt64 operator -( EstimateInt64 value, long scalar )
    {
      EstimateInt64.CheckForNull( value, "value" );

      return ( EstimateInt64.IsAdditiveIdentity( scalar ) )
               ? value
               : new EstimateInt64( value.Value - scalar, value.IsEstimate );
    }

    public static EstimateInt64 operator -( EstimateInt64 self )
    {
      EstimateInt64.CheckForNull( self, "self" );

      return ( EstimateInt64.IsAdditiveIdentity( self ) )
               ? self
               : new EstimateInt64( -self.Value, self.IsEstimate );
    }

    public static EstimateInt64 operator *( EstimateInt64 value, long scalar )
    {
      EstimateInt64.CheckForNull( value, "value" );

      return ( EstimateInt64.IsMultiplicativeIdentity( scalar ) )
               ? value
               : new EstimateInt64( value.Value * scalar, value.IsEstimate );
    }

    private static void CheckForNull( EstimateInt64 value, string parameterName )
    {
      if( value == null )
        throw new ArgumentNullException( parameterName );
    }

    private static bool IsBecomingEstimate( EstimateInt64 first, EstimateInt64 second )
    {
      return ( first.IsEstimate || second.IsEstimate );
    }

    private static bool IsAdditiveIdentity( EstimateInt64 value )
    {
      return ( EstimateInt64.IsAdditiveIdentity( value.Value ) && !value.IsEstimate );
    }

    private static bool IsAdditiveIdentity( long scalar )
    {
      return ( scalar == 0 );
    }

    private static bool IsMultiplicativeIdentity( EstimateInt64 value )
    {
      return ( EstimateInt64.IsMultiplicativeIdentity( value.Value ) && !value.IsEstimate );
    }

    private static bool IsMultiplicativeIdentity( long scalar )
    {
      return ( scalar == 1 );
    }
  }
}
